<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="108px"
    >
      <el-form-item label="编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文本" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入文本"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="标签列表" prop="tags">
        <el-select class="!w-240px" filterable multiple v-model="queryParams.tags" placeholder="请选择标签">
          <el-option v-for="item in tagList" :key="item.id" :value="item.id" :label="item.tagName" />
        </el-select>
      </el-form-item>
      <el-form-item label="关键字列表" prop="keyworlds">
        <el-select class="!w-240px" filterable multiple v-model="queryParams.keyworlds" placeholder="请选择关键字">
          <el-option v-for="item in keywordList" :key="item.id" :value="item.id" :label="item.keyWord" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['audiotext:audio-text:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openTransformForm"
          v-hasPermi="['audiotext:audio-text:create']"
        >
          ⽣成⾳频
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true"
              @selection-change="handleSelectionChange"
              :show-overflow-tooltip="true">
      <el-table-column type="selection" width="55" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="文本内容" align="center" prop="content" />
      <el-table-column label="标签列表" align="center" prop="tagList" >
        <template #default="scope">
        <el-tag v-for="item in scope.row.tagList" :key="item.id" type="success">{{ item.tagName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="关键字列表" align="center" prop="keyworldList" >
        <template #default="scope" >
          <span v-if="scope.row.keyworldList">
          <el-tag v-for="item in scope.row.keyworldList" :key="item.id" type="success">{{ item.keyWord }}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="关联类型" align="center" prop="relationType" >
        <template #default="scope">
          <el-tag v-if="scope.row.relationType === 1" type="success">标准回复</el-tag>
          <el-tag v-else-if="scope.row.relationType === 2" type="warning">问句回复</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetailDialog(scope.row.id)"
            v-hasPermi="['audiotext:audio-text:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['audiotext:audio-text:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['audiotext:audio-text:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <AudioTextForm ref="formRef" @success="getList" />
  <Text2AudioForm ref="text2AudioRef"/>
  <Text2AudioDetail ref="text2AudioDetailRef"/>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { AudioTextApi, AudioTextVO } from '@/api/lm/audiotext/index'
import AudioTextForm from './AudioTextForm.vue'
import Text2AudioForm from './components/Text2AudioForm.vue'
import Text2AudioDetail from './components/Text2AudioDetail.vue'
import {TagApi} from "@/api/lm/tag";
import {KeyWordApi} from "@/api/lm/keyword";

/** 文本编辑 列表 */
defineOptions({ name: 'AudioText' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<AudioTextVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 100,
  id: undefined,
  content: undefined,
  tags: undefined,
  keyworlds: undefined,
})
const keywordList = ref([]);
const tagList = ref([]);
const queryFormRef = ref() // 搜索的表单
const text2AudioRef = ref() //
const text2AudioDetailRef = ref() //
const selectedIdList = ref([]);

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AudioTextApi.getAudioTextPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id, {
    tagList: tagList.value,
    keywordList: keywordList.value
  })
}

const openTransformForm = ()=> {
  if(selectedIdList.value.length == 0) {
    message.error("请选择要转换的文本");
    return;
  }

  text2AudioRef.value.open({
    textIdList: selectedIdList.value
  });
}

const openDetailDialog = (id)=> {
  text2AudioDetailRef.value.open(id);
}

const handleSelectionChange = (val)=> {
  selectedIdList.value = val.map(e=> {
    return e.id;
  });
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AudioTextApi.deleteAudioText(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()

  // 获取标签列表
  TagApi.getTagPage({
    pageNo: 1,
    pageSize: 100
  }).then(e=> {
    tagList.value = e.list;
  })
  // 获取关键字列表
  KeyWordApi.getKeyWordPage({
    pageNo: 1,
    pageSize: 100
  }).then(e=> {
    keywordList.value = e.list;
  });
})
</script>
